﻿using System;
using System.Collections.Generic;
using Assets.AnimationPlayer.Scripts.Animations;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Assets.AnimationPlayer.Scripts.UI
{
    public enum LogoColorType
    {
        White = 0,
        Color = 1,
        All = 2
    }

    public class AnimationVar : MonoBehaviour
    {
        public static UnDestoryScene UnDestoryScene;
        public static AnimationPlayCfg AnimationPlayCfg = new AnimationPlayCfg();
        public static float ScaleNum;


        public static int PlayeTimes{
            get{
                return UnDestoryScene.CallApi.AutoPlayModel ? 3 : int.MaxValue;
            }
        }

        public static void ReleaseResources()
        {
            var objAry = Resources.FindObjectsOfTypeAll<Material>();
            for (int i = 0; i < objAry.Length; ++i)
            {
                objAry[i] = null;//解除资源的引用
            }

            //卸载没有被引用的资源
            Resources.UnloadUnusedAssets();

            //立即进行垃圾回收
            GC.Collect();
            GC.WaitForPendingFinalizers();//挂起当前线程，直到处理终结器队列的线程清空该队列为止
            GC.Collect();
        }
        //显示动画列表
        public static void BackToList()
        {
            AnimationPlayCfg.NextSceneName = "AnimationPlayer/Scene/AnimationListView";
            var asyncAction = SceneManager.LoadSceneAsync(AnimationPlayCfg.NextSceneName, LoadSceneMode.Single);
            if (asyncAction.isDone)
            {
                UnDestoryScene.CallApi.ShouldReset = true;
                ReleaseResources();
            }
        }
        //播放动画
        public static void PlayAnimation(GameObject go = null)
        {
            AnimationPlayCfg.NextSceneName = "AnimationPlayer/Scene/AnimationView";
            var asyncAction =  SceneManager.LoadSceneAsync(AnimationPlayCfg.NextSceneName, LoadSceneMode.Single);
            if (asyncAction.isDone)
            {
                ReleaseResources();
            }
        }

        //重置静态变量
        public static void ResetVar()
        {
            AnimationPlayCfg = new AnimationPlayCfg();
        }

        public static string GetFilePath(string fileName)
        {

            string filePath =
#if UNITY_ANDROID && !UNITY_EDITOR
        "jar:file://" + Application.dataPath + "!/assets/";  
#elif UNITY_IPHONE && !UNITY_EDITOR
        Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
                "file://" + Application.dataPath + "/StreamingAssets/";
#else
        string.Empty;  
#endif
            //Debug.Log(filePath + fileName);
            return filePath + fileName;
        }

    }

    public class AnimationPlayCfg
    {
        public AnimationObj SelectAnimationObj
        {
            get
            {
                try
                {
                     return Cfg.AnimationObjs[SelectAnimationIndex];
  
                }
                catch (Exception)
                {
                   return null;
                }
            }
        }
        //下一个场景的名称
        public string NextSceneName;

        public int SelectAnimationIndex;
        public LogoColorType ColorLogoType;
        public AnimationCfg Cfg;
        public Dictionary<string,Texture2D> ImageCacheDictionary;
        public AssetBundle ImageAssetBundle;
        public bool AutoPlay;

        public AnimationPlayCfg()
        {
            NextSceneName = "";
            SelectAnimationIndex = -1;
            ColorLogoType = LogoColorType.All;
            Cfg = new AnimationCfg();
            ImageCacheDictionary = new Dictionary<string, Texture2D>();
            ImageAssetBundle = null;
            AutoPlay = false;
        }

    }



}
